package com.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ListPosition;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.params.SetParams;

import java.util.*;

public class TestJedis {

    public static void main(String[] args) {

        Jedis jedis1 = new Jedis("127.0.0.1", 6796);
        jedis1.auth("1Qaz2Wsx3Edc");
        jedis1.connect();
//        for (int i = 0; i < 30; i++) {
//            try {
//                jedis1.connect();
//                System.out.println("Fadis连接成功");
//                break;
//            }catch (Exception e){
//                System.out.println("Fadis连接失败，1s后自动重连--" + i);
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException ex) {
//                    ex.printStackTrace();
//                }
//            }
//        }

        jedis1.flushAll();

        Jedis jedis2 = new Jedis("r-8vbrz1v3smlbjqeuatpd.redis.zhangbei.rds.aliyuncs.com", 6379);
        jedis2.auth("1Qaz2Wsx3Edc");
        jedis2.connect();
        System.out.println("Redis连接成功");
        jedis2.flushAll();

        // 测string指令
        if(1 + 1 == 2){
            System.out.println("------------------string-----------------");
            testString(jedis1, true);
            testString(jedis2, false);
            System.out.println("-----------------------------------------");
        }

        // 测hash指令
        if(1 + 1 == 3){
            System.out.println("------------------hash-----------------");
            testHash(jedis1, true);
            testHash(jedis2, false);
            System.out.println("-----------------------------------------");
        }

        // 测hyperloglog指令
        if(1 + 1 == 3){
            System.out.println("------------------hyperloglog-----------------");
            testpf(jedis1, true);
            testpf(jedis2, false);
            System.out.println("-----------------------------------------");
        }

        // 测List指令
        if(1 + 1 == 3){
            System.out.println("------------------List-----------------");
            testList(jedis1, true);
            testList(jedis2, false);
            System.out.println("-----------------------------------------");
        }

        // 测Set指令
        if(1 + 1 == 3){
            System.out.println("------------------St-----------------");
            testSet(jedis1, true);
            testSet(jedis2, false);
            System.out.println("-----------------------------------------");
        }

        // 测ZSet指令
        if(1 + 1 == 3){
            System.out.println("------------------St-----------------");
            testZSet(jedis1, true);
            testZSet(jedis2, false);
            System.out.println("-----------------------------------------");
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(60 * 60 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).run();
    }


    private static void sout(String cmd, Object o){
        if( o == null) {
            System.out.println(cmd + "--(null)");
            return;
        }
        if(o instanceof List || o instanceof Set){
            Collection c = (Collection)o;
            System.out.print(cmd + "--[");
            for (Object e: c){
                System.out.print(e + ", ");
            }
            System.out.println("]");
        }else if(o instanceof Map){
            Map m = (Map) o;
            System.out.print(cmd + "--[");
            for (Object k: m.keySet()){
                System.out.print(k + ": " + m.get(k) + ", ");
            }
            System.out.println("]");
        }else{
            System.out.println(cmd + "--" + o);
        }
    }

    private static void sout(Object o){
        sout("", o);
    }


    private static void testString(Jedis jedis, boolean isFadis){
        if(isFadis) System.out.println("~~~~ Fadis测试 ~~~~");
        else System.out.println("~~~~ Redis测试 ~~~~");
        try {
            sout(jedis.set("k1", "v1"));
            sout(jedis.setnx("k1", "ssss1111"));
            sout(jedis.setex("k1", 5, "v3"));
            sout(jedis.append("k1", "v2"));
            sout(jedis.get("k1"));

            sout(jedis.incr("k2"));
            sout(jedis.incrBy("k2", 100));
            sout(jedis.decr("k2"));
            sout(jedis.decrBy("k2", 30));
            sout(jedis.incrByFloat("k2", 2.1));

            sout(jedis.strlen("k1"));
            sout(jedis.setrange("k1", 2, "1122334"));
            sout(jedis.getrange("k1", 1, 4));

            sout(jedis.exists("k1"));
            sout(jedis.del("k1", "k2"));

            sout(jedis.set("k3", "v3", SetParams.setParams().ex(5).nx()));
            sout(jedis.get("k3"));
            sout(jedis.ttl("k3"));

            sout(jedis.mset("k1", "v1", "k2", "v2"));
            sout(jedis.mget("k1", "k2"));

            sout(jedis.del("k1", "k2"));
            sout(jedis.rpush("k1", "v1", "v2"));
            sout(jedis.msetnx("k1", "v1", "k2", "v2"));
            sout(jedis.mget("k1", "k2"));

        }catch (Exception e){
            System.out.println((isFadis ? "Fadis" : "Redis") + "出错：");
            e.printStackTrace();
        }

    }

    private static void testHash(Jedis jedis, boolean isFadis){
        if(isFadis) System.out.println("~~~~ Fadis测试 ~~~~");
        else System.out.println("~~~~ Redis测试 ~~~~");
        try {
            sout(jedis.hset("h1", "f1", "v1"));
            sout(jedis.hsetnx("h1", "f1", "v1"));
            sout(jedis.hsetnx("h1", "f2", "v2"));

            HashMap<String, String> m = new HashMap<>();
            m.put("a", 1+"");
            m.put("b", 2+"");
            sout(jedis.hmset("hash1", m));

            sout(jedis.hget("h1", "f1"));
            sout(jedis.hget("h1", "f3"));
            sout(jedis.hget("h2", "f3"));
            sout(jedis.hexists("h1", "f1"));
            sout(jedis.hexists("h2", "f2"));
            sout(jedis.hgetAll("h1"));
            sout(jedis.hkeys("h1"));
            sout(jedis.hvals("h1"));

            sout(jedis.hlen("h1"));
            sout(jedis.hlen("h8"));
            sout(jedis.hstrlen("h1", "f1"));
            sout(jedis.hstrlen("h1", "f8"));
            sout(jedis.hstrlen("h8", "f8"));

            sout(jedis.hincrBy("h1", "h5", 11));
            sout(jedis.hincrByFloat("h1", "h5", 11.3));
            sout(jedis.hincrByFloat("h1", "f1", 11.3));


        }catch (Exception e){
            System.out.println((isFadis ? "Fadis" : "Redis") + "出错：");
            e.printStackTrace();
        }
    }



    private static void testpf(Jedis jedis, boolean isFadis){
        if(isFadis) System.out.println("~~~~ Fadis测试 ~~~~");
        else System.out.println("~~~~ Redis测试 ~~~~");
        try {
            sout(jedis.pfadd("ip_20200212", "1", "2", "3", "4", "5"));
            sout(jedis.pfcount("ip_20200212"));

            sout(jedis.pfadd("ip_20200213", "5", "6", "7", "7", "9"));
            sout(jedis.pfcount("ip_20200212", "ip_20200213"));

            sout(jedis.pfmerge("ip_2_days", "ip_20200212", "ip_20200212"));
            sout(jedis.pfcount("ip_2_days"));

        }catch (Exception e){
            System.out.println((isFadis ? "Fadis" : "Redis") + "出错：");
            e.printStackTrace();
        }
    }

    private static void testbloom(Jedis jedis, boolean isFadis){
        if(isFadis) System.out.println("~~~~ Fadis测试 ~~~~");
        else System.out.println("~~~~ Redis测试 ~~~~");
        try {
            //sout(jedis.bf);

        }catch (Exception e){
            System.out.println((isFadis ? "Fadis" : "Redis") + "出错：");
            e.printStackTrace();
        }
    }

    private static void testList(Jedis jedis, boolean isFadis){
        if(isFadis) System.out.println("~~~~ Fadis测试 ~~~~");
        else System.out.println("~~~~ Redis测试 ~~~~");
        try {
            sout(jedis.rpush("t1", "v3", "v4"));
            sout(jedis.lpush("t1", "v2", "v1"));
            sout(jedis.linsert("t1", ListPosition.AFTER, "v4", "v5"));
            sout(jedis.linsert("t1", ListPosition.BEFORE, "v1", "v0"));
            sout(jedis.lset("t1", 0, "v00"));
//            sout(jedis.lset("t1", 100, "v100"));
            sout(jedis.lrange("t1", 0, -1));
            sout(jedis.llen("t1"));
            sout(jedis.lindex("t1", -1));
            sout(jedis.lindex("t1", 100));

            sout(jedis.lpop("t1"));
            sout(jedis.rpop("t1"));
            sout("rpoplpush", jedis.rpoplpush("t1", "t2"));
            sout(jedis.lrange("t1", 0, -1));
            sout(jedis.lrange("t2", 0, -1));

            sout("ltrim", jedis.ltrim("t1", 0, 0));
            sout("lrange--trim之后", jedis.lrange("t1", 0, -1));
            sout("lrange--不存在的key", jedis.lrange("ttttt", 0, -1));

            sout(jedis.rpush("k3", "v0", "v1", "v1", "v2", "v1", "v1", "v3"));
            sout("当前列表", jedis.lrange("k3", 0, -1));
            sout("lrem", jedis.lrem("k3", -3, "v1"));
            sout("当前列表", jedis.lrange("k3", 0, -1));


        }catch (Exception e){
            System.out.println((isFadis ? "Fadis" : "Redis") + "出错：");
            e.printStackTrace();
        }
    }

    private static void testSet(Jedis jedis, boolean isFadis){
        if(isFadis) System.out.println("~~~~ Fadis测试 ~~~~");
        else System.out.println("~~~~ Redis测试 ~~~~");
        try {
            sout(jedis.sadd("s1", "v1", "v2", "v3", "v4", "v1"));
            sout(jedis.smembers("s1"));
            sout("scard", jedis.scard("s1"));
            sout(jedis.sismember("s1", "v3"));
            sout(jedis.sismember("s1", "v5"));
            sout(jedis.sismember("s2", "xx"));
            sout("spop", jedis.spop("s1"));
            sout(jedis.smembers("s1"));
            sout("srandmember", jedis.srandmember("s1", 2));

            sout("srem", jedis.srem("s1", "v1", "v2", "v3", "v4", "v5"));
            sout("scard", jedis.scard("s1"));


            sout(jedis.sadd("c1", "v1", "v2", "v3"));
            sout(jedis.sadd("c2", "v4", "v2", "v3"));

            sout("sinter", jedis.sinter("c1", "c2"));
            sout("sunion", jedis.sunion("c1", "c2"));
            sout("sdiff", jedis.sdiff("c1", "c2"));

            sout("sinterstore", jedis.sinterstore("cc1","c1", "c2"));
            sout(jedis.smembers("cc1"));
            sout("sunionstore", jedis.sunionstore("cc1","c1", "c2"));
            sout(jedis.smembers("cc1"));
            sout("sdiffstore", jedis.sdiffstore("cc1","c1", "c2"));
            sout(jedis.smembers("cc1"));

        }catch (Exception e){
            System.out.println((isFadis ? "Fadis" : "Redis") + "出错：");
            e.printStackTrace();
        }
    }

    private static void testZSet(Jedis jedis, boolean isFadis){
        if(isFadis) System.out.println("~~~~ Fadis测试 ~~~~");
        else System.out.println("~~~~ Redis测试 ~~~~");
        try {
            sout(jedis.zadd("s1", 11, "a"));
            HashMap<String, Double> m = new HashMap<>();
            m.put("c", 13.0);
            m.put("d", 14.0);
            m.put("b", 12.0);
            m.put("f", 16.0);
            m.put("e", 15.0);
            sout(jedis.zadd("s1", m));
            sout(jedis.zrange("s1", 0, 30));

            sout("zcard", jedis.zcard("s1"));
            sout("zcount", jedis.zcount("s1", 12.1, 14.6));

            sout(jedis.zrank("s1", "e"));
            sout(jedis.zrevrank("s1", "e"));

            sout("zscore", jedis.zscore("s1", "e"));
            sout("zincrby", jedis.zincrby("s1", -30, "e"));
            sout("zscore", jedis.zscore("s1", "e"));

            sout("zrank", jedis.zrank("s1", "e"));
            sout("zrevrank", jedis.zrevrank("s1", "e"));

            sout(jedis.zrange("s1", 0, 30));

            sout("zrange", jedis.zrange("s1", 0, 4));
            sout("zrangeWithScores", jedis.zrangeWithScores("s1", 0, 4));

            sout("zrangeByScore", jedis.zrangeByScore("s1", "-100", "15"));
            sout("zrangeByScoreWithScores", jedis.zrangeByScoreWithScores("s1", "-100", "15"));
//            sout("zrangeByLex", jedis.zrangeByLex("s1", "a", "d"));

            sout("zrevrangeByScore", jedis.zrevrangeByScore("s1", "15", "-100"));
            sout("zrevrangeByScoreWithScores", jedis.zrevrangeByScoreWithScores("s1", "15", "-100"));

            sout(jedis.zrem("s1", "e", "c"));
            sout(jedis.zrange("s1", 0, -1));

            sout(jedis.zremrangeByScore("s1", 12.3, 14.5));
            sout(jedis.zrangeWithScores("s1", 0, -1));

            sout(jedis.zremrangeByRank("s1", 0, 1));
            sout(jedis.zrangeWithScores("s1", 0, -1));


            sout(jedis.zadd("ss1", 1, "a"));
            sout(jedis.zadd("ss1", 2, "b"));
            sout(jedis.zadd("ss1", 3, "c"));

            sout(jedis.zadd("ss2", 2, "b"));
            sout(jedis.zadd("ss2", 3, "c"));
            sout(jedis.zadd("ss2", 4, "d"));

            sout(jedis.zinterstore("sinter", new ZParams().weights(1, 1.5).aggregate(ZParams.Aggregate.MAX), "ss1", "ss2"));
            sout(jedis.zrangeWithScores("sinter", 0, -1));

            sout(jedis.zunionstore("sunion", new ZParams().weights(1, 1.5).aggregate(ZParams.Aggregate.MAX), "ss1", "ss2"));
            sout(jedis.zrangeWithScores("sunion", 0, -1));


        }catch (Exception e){
            System.out.println((isFadis ? "Fadis" : "Redis") + "出错：");
            e.printStackTrace();

            BitSet b = BitSet.valueOf(new byte[0]);


        }
    }
}
